package cn.qiao.demo.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * desc:
 *
 * @author qiao
 * @date 2023年12月13日 22:13
 */
public class Test2 {


    public static void main(String[] args) throws Exception {
        Method queryPushSuccessRecordByPage = Test.class.getMethod("queryPushSuccessRecordByPage", int.class, int.class);
        Type returnType = queryPushSuccessRecordByPage.getGenericReturnType();
        Type[] actualTypeArguments = ((ParameterizedTypeImpl) returnType).getActualTypeArguments();
        Object listPunishPageList = new PunishPageList<List<PubishSuccessDto>>();
        Object o = ((ParameterizedTypeImpl) returnType).getRawType().newInstance();
        String s = getString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        Class<?> aClass = listPunishPageList.getClass();
        Method[] declaredMethods = aClass.getMethods();
        for (Method method : declaredMethods) {
            String name = method.getName();
            if (name.contains("set")){
                String replace = name.replace("set", "get");
                String set = name.replace("set", "");
                String field = lowercaseFirstLetter(set);
                for (Method declaredMethod : declaredMethods) {
                    if (declaredMethod.getName().equals(replace)) {
                        Type genericReturnType = declaredMethod.getGenericReturnType();
                        if (genericReturnType instanceof TypeVariable) {
                            Object object = jsonObject.getObject(field, actualTypeArguments[0]);
                            method.invoke(o,object);
                        }else {
                            Class<?>[] parameterTypes = method.getParameterTypes();
                            Object object = jsonObject.getObject(field,parameterTypes[0]);
                            method.invoke(o,object);
                        }
                    }
                }

            }
        }
        System.out.println(o);

    }

    // 获取泛型签名信息
    private static String getGenericSignature(Type type) {
        if (type instanceof TypeVariable) {
            // 如果是类型变量
            TypeVariable<?> typeVariable = (TypeVariable<?>) type;
            return typeVariable.getName();
        } else if (type instanceof Class) {
            // 如果是普通类
            Class<?> clazz = (Class<?>) type;
            return clazz.getSimpleName();
        } else {
            // 其他情况
            return type.getTypeName();
        }
    }


    // 判断类型是否为泛型类型
    private static boolean isGenericParameter(Class<?> parameterType) {
        Type[] genericInterfaces = parameterType.getGenericInterfaces();
        Type genericSuperclass = parameterType.getGenericSuperclass();

        // 判断是否是泛型类型
        return genericInterfaces.length > 0 || genericSuperclass != null;
    }

    private static String lowercaseFirstLetter(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        // 将字符串的第一个字符转为小写
        char firstChar = Character.toLowerCase(str.charAt(0));

        // 构建新的字符串
        return firstChar + str.substring(1);
    }

    private static String getString() {
        PunishPageList riskctrlResult = new PunishPageList<>();
        ArrayList<PubishSuccessDto> objects = new ArrayList<PubishSuccessDto>();
        for (int i = 0; i < 2; i++) {
            PubishSuccessDto pubishSuccessDto = new PubishSuccessDto();
            pubishSuccessDto.setRoleId(""+i);
            pubishSuccessDto.setGmtCreateFrom("时间"+i);
            pubishSuccessDto.setGmtCreateTo("GmtCreateTo"+i);
            pubishSuccessDto.setGmtExecutFrom("setGmtExecutFrom");
            String s = LocalDateTime.now().toString();
            pubishSuccessDto.setGmtExecutTo(s);
            objects.add(pubishSuccessDto);
        }
        riskctrlResult.setData(objects);
        Paginator paginator = new Paginator();
        paginator.setAge(1);
        paginator.setItems(20);
        paginator.setItemsPages(10);
        riskctrlResult.setPaginator(paginator);
        riskctrlResult.setErrorContext("errorContext");
        riskctrlResult.setSuccess(true);
        return JSON.toJSONString(riskctrlResult);
    }

}
